<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\request;
use PHPExcel_IOFactory;
use PHPExcel;
use dh2y\login\login;
use think\captcha\Captcha;
use think\captcha\CaptchaController;
use think\Session;
class Index extends controller
{
	public function _initialize()
    {
		$menuinfo=Db::name('menuinfo')->order('sort')->select();//后台管理左侧菜单加载
		$this->assign("menuinfo",$menuinfo);
        $this->template='index';
		$setting=Db::name('settinginfo')->where('id',Config()['wid'])->find();//网站配置加载
		$navinfo=Db::name('newscateinfo')->where(['wid'=>Config()['wid'],'show'=>1])->select();//后台管理左侧菜单加载
		
		$this->assign("navinfo",$navinfo);
		$this->assign('setting',$setting);	
    }
	
    public function index()
    {
		
		$d=dir(ROOT_PATH.'public\template\index\common');
		$i=0;
		while (($file = $d->read()) !== false){
		  $len=strlen($file);
		  $refile[$i]=substr($file,0,$len-5);
		  $i++;
		}
		unset($refile[0]);
		unset($refile[1]);
		$refile=array_values($refile);
		
		$d->close();
		
		$this->assign('refilejson',json_encode($refile));
		$this->assign('refile',$refile);
		return $this->fetch('index/index');

    }
	public function ProduceTemplate()
	{
		//模拟数据 首页模板
		$include[0]='{include file="index/common/nav" /}';
		$include[1]='{include file="index/common/banner" /}';
		
		$include[2]='{include file="index/common/footer" /}';
		$t1='';
		
		$template=fopen(ROOT_PATH.'public/template/index/xx.html','w');
		foreach($include as $key=>$val)
		{
		$t1=$t1.$val;
		}
		$t2='{extend name="index/common/common"/}{block name="main"}'.$t1."\n".'{/block}';
		fwrite($template,$t2);
		fclose($template);
		$this->success('生成模板成功！');
	}
	public function page($page)
	{
		$file=fopen(ROOT_PATH.'public/template/index/common/img.html','r');
		$s=fread($file,10000);
		
		return $this->fetch($page);
	}
	public function dd($ck)
	{
		$str='{include file="index/common/header" /} ';
		$this->assign('ck',$str);
		return $this->fetch('index/ck');

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public function phpexcel()
	{
		$path = dirname(__FILE__); //找到当前脚本所在路径
  $PHPExcel = new PHPExcel(); //实例化PHPExcel类，类似于在桌面上新建一个Excel表格
  $PHPSheet = $PHPExcel->getActiveSheet(); //获得当前活动sheet的操作对象
  $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  $PHPSheet->setCellValue('A1','姓名')->setCellValue('B1','分数');
  $PHPSheet->setCellValue('A2','张三')->setCellValue('B2','50');
  $PHPWriter = PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');//按照指定格式生成Excel文件，‘Excel2007’表示生成2007版本的xlsx，‘Excel5’表示生成2003版本Excel文件
  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告诉浏览器输出07Excel文件
//header('Content-Type:application/vnd.ms-excel');//告诉浏览器将要输出Excel03版本文件
        header('Content-Disposition: attachment;filename="01simple.xlsx"');//告诉浏览器输出浏览器名称
        header('Cache-Control: max-age=0');//禁止缓存
        $PHPWriter->save("php://output");

	}
}
